<template>
  <!-- 菜品规格弹窗 -->
  <view v-if="visible" class="spec-modal-overlay">
    <view class="spec-modal-content">
      <scroll-view scroll-y class="modal-scroll">
        <!-- 顶部：图片展示区 -->
        <view class="image-section">
          <image :src="`https://rsl-plus.oss-rg-china-mainland.aliyuncs.com/${dishData.dishImage}`" class="product-image" mode="aspectFill" />
          <view class="overlay-icons">
            <view class="icon-btn close-btn" @click="closeModal">×</view>
          </view>
        </view>

        <!-- 商品信息 -->
        <view class="product-detail-info">
          <view class="title-row">
            <text class="product-modal-title">{{ dishData.dishName }}</text>
            <text class="product-price">¥{{ dishData.price }}</text>
          </view>
          <text class="product-description">{{ dishData.dishDescribe }}</text>
        </view>
      </scroll-view>

      <!-- 底部：数量选择、操作按钮 -->
      <view class="modal-footer">
        <view class="quantity-section">
          <text class="quantity-label">数量</text>
          <view class="quantity-selector">
            <view class="quantity-btn" @click="decreaseQuantity">-</view>
            <text class="quantity-value">{{ quantity }}</text>
            <view class="quantity-btn" @click="increaseQuantity">+</view>
          </view>
        </view>
        <button class="action-btn add-to-cart-btn" @click="addToCart">加入购物车</button>
      </view>
    </view>
  </view>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue';

const props = defineProps({
  visible: { type: Boolean, default: false },
  dishData: { type: Object, default: () => ({}) }
});

const emit = defineEmits(['close', 'add-to-cart']);

// 监听visible变化，当弹窗打开时重置数量
watch(() => props.visible, (newVal) => {
  if (newVal) {
    quantity.value = 1
  }
})

const closeModal = () => {
  emit('close');
};

// 数量选择器相关
const quantity = ref(1)

// 减少数量
function decreaseQuantity() {
  if (quantity.value > 1) {
    quantity.value--
  }
}

// 增加数量
function increaseQuantity() {
  quantity.value++
}

// 加入购物车
function addToCart() {
  emit('add-to-cart', {
    ...props.dishData,
    quantity: quantity.value
  })
  closeModal()
}
</script>

<style lang="scss" scoped>
.spec-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  justify-content: center;
  align-items: flex-end;
  z-index: 1000;
}

.spec-modal-content {
  background: #fff;
  width: 100%;
  max-height: 90vh;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-scroll {
  flex: 1;
  max-height: calc(90vh - 120px); /* 减去底部操作区域的高度 */
}

.image-section {
  position: relative;
  width: 100%;
  padding-top: 100%;
  background: #f8f8f8;
  overflow: hidden;
}

.product-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 20px 20px 0 0;
  background: #fff;
}

.overlay-icons {
  position: absolute;
  top: 16px;
  right: 16px;
  display: flex;
  gap: 12px;
  z-index: 10;
}

.icon-btn {
  width: 36px;
  height: 36px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
  color: #666;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.product-detail-info {
  padding: 20px;
  background: #fff;
}

.title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.product-modal-title {
  font-size: 20px;
  font-weight: bold;
  color: #333;
  flex: 1;
  margin-right: 12px;
}

.product-price {
  font-size: 20px;
  font-weight: bold;
  color: #ff6b6b;
  flex-shrink: 0;
}

.product-description {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
}

.modal-footer {
  padding: 20px;
  background: #fff;
  border-top: 1px solid #f0f0f0;
  flex-shrink: 0;
}

.quantity-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.quantity-label {
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

.quantity-selector {
  display: flex;
  align-items: center;
  border: 1px solid #eee;
  border-radius: 20px;
  overflow: hidden;
}

.quantity-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f8f8f8;
  font-size: 18px;
  color: #666;
  transition: all 0.3s;
  
  &:active {
    background: #f0f0f0;
  }
}

.quantity-value {
  min-width: 40px;
  text-align: center;
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.action-btn {
  width: 100%;
  height: 44px;
  border-radius: 22px;
  font-size: 16px;
  font-weight: 500;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.add-to-cart-btn {
  background: #008CB5;
  color: #fff;
  
  &:active {
    opacity: 0.9;
  }
}
</style> 